package net.jxta.myjxta.presence;

import net.jxta.myjxta.util.Peer;
import net.jxta.myjxta.util.objectmodel.PeerNode;

import javax.swing.*;
import java.text.Collator;

public final class PeerEntry implements Comparable {
    public final Peer m_peer;


    public PeerNode getNode() {
        return m_node;
    }

    private final PeerNode m_node;

    /**
     * @param m_peer
     */
    public PeerEntry(PeerNode m_peer) {
        super();
        this.m_peer = m_peer.getPeer();
        this.m_node = m_peer;
        updateLastSeen(null);
    }

    public void updateLastSeen(PeerStatus status) {
        m_node.informAccessed();
        m_node.setInfo(PeerNode.STATUS_PRESENCE, status, true);
    }

    /* (non-Javadoc)
        * @see java.lang.Object#equals(java.lang.Object)
        */
    public boolean equals(Object arg0) {

        if (!(arg0 instanceof PeerEntry)) {
            return false;
        }

        return m_peer.equals(((PeerEntry) arg0).m_peer);
    }

    /* (non-Javadoc)
        * @see java.lang.Object#hashCode()
        */
    public int hashCode() {
        return m_peer.hashCode();
    }

    public int compareTo(Object o) {
        if (o instanceof PeerEntry) {
            PeerEntry otherPeerEntry = (PeerEntry) o;
            if (otherPeerEntry.m_peer != null && otherPeerEntry.m_peer.getName() != null) {
                Collator myCollator = Collator.getInstance();
                return myCollator.compare(this.m_peer.getName(), otherPeerEntry.m_peer.getName());
            } else {
                return 1;
            }
        } else {
            return 1;
        }


    }

    public Icon getIcon(boolean p_selected, boolean p_hasFocus) {
        return m_node.getImage(p_selected, false, p_hasFocus);
    }

    public long getLastSeen() {
        // TODO Auto-generated method stub
        return m_node.getLastAccessTimestamp();
    }
}
